package com.youaishujuhui.devops.web.util.host;

import java.io.IOException;

import org.springframework.web.socket.WebSocketSession;

import com.youaishujuhui.devops.web.cache.GlobalCache;
import com.youaishujuhui.devops.web.util.Common;
import com.youaishujuhui.devops.web.util.WsUtil;

import lombok.AllArgsConstructor;
import net.schmizz.sshj.common.StreamCopier.Listener;

@AllArgsConstructor
public class HostInstallTransferProgressListener implements Listener {
	
	private String ip;
	
	private String userName;
	
	private long size;
	
	private long lastSendTime;

	@Override
	public void reportProgress(long transferred) throws IOException {
		long now = System.currentTimeMillis();
		
		if (now - lastSendTime > 500) {
			WebSocketSession session = GlobalCache.wsSessionCacheMap.get(userName);
			//session.sendMessage(new TextMessage("已完成：" + String.valueOf((int)((double) transferred / size * 100)) + "%"));
			WsUtil.send(session, Common.HOST_INIT_DIV_ID_PREFIX + ip, "已完成：" + String.valueOf((int)((double) transferred / size * 100)) + "%");
			
			lastSendTime = now;
		}
		
	}

}
